package think.in.java.concurrency.practise.exec4;

import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 * User: Richard
 * Date: 5/19/11
 * Time: 8:44 AM
 * To change this template use File | Settings | File Templates.
 */
public class Daemons{
      public static void main(String[] args) throws InterruptedException {
          Thread mainThread = new Thread(new Daemon());
          mainThread.setDaemon(true);
          mainThread.start();
          Object object = new Object();
          object.wait();
          System.out.println("Test Start!");
          TimeUnit.SECONDS.sleep(5000);

      }
}

class Daemon implements Runnable{
    private Thread[] threads = new Thread[10];
    public void run() {
        for(int i=0;i<10;i++){
           threads[i]= new Thread(new DaemonSpawn());
            //there wwe do't need to set the daemon at the parent thread is a daemon
            //threads[i].setDaemon(true);
            threads[i].start();
            System.out.println("DaemonSwpan " + i + " Start!");
        }
        for(int i=0; i<threads.length;i++){
            System.out.println("threads " + i +" is a daemon " + threads[i].isDaemon());
        }
        while (true){
            Thread.yield();
            for(int i=0; i<threads.length;i++){
            System.out.println("threads " + i +" is a daemon " + threads[i].isDaemon());
        }
        }
    }
}

class DaemonSpawn implements Runnable{

    public void run() {
        while (true){
            Thread.yield();
        }
    }
}


